//
//  TweetTableViewCell.m
//  OSChina
//
//  Created by 8b410-2 on 16/12/26.
//  Copyright © 2016年 abb. All rights reserved.
//

#import "OSCTweetTableViewCell.h"
#import "UILabel+Common.h"
#import "UIColor+Common.h"
#import "UIImageView+SDWebImage.h"
#import "OSCTweetViewModel.h"
#import "PYPhotoBrowser.h"
@interface OSCTweetTableViewCell ()
/// 动弹viewModel
@property (nonatomic, strong) OSCTweetViewModel *viewModel;
/// 头像view
@property (nonatomic, strong) UIImageView *iconView;
/// 昵称Label
@property (nonatomic, strong) UILabel *nameLabel;
/// 正文Label
@property (nonatomic, strong) UILabel *contentLabel;
/// 时间Label
@property (nonatomic, strong) UILabel *timeLabel;
/// 评论Button
@property (nonatomic, strong) UIButton *commentButton;
/// 图片相册
@property (nonatomic, strong) PYPhotosView *tweetPhotoView;

@end

@implementation OSCTweetTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self setupUI];
    }
    return self;
}

- (void)setupUI {
    _iconView = [[UIImageView alloc] init];
    [self.contentView addSubview:_iconView];
    
    _nameLabel = [UILabel labelWithSystemFontSize:16 textColor:[UIColor titleColor]];
    [self.contentView addSubview:_nameLabel];
    
    _contentLabel = [UILabel labelWithSystemFontSize:16 textColor:[UIColor contentTextColor]];
    _contentLabel.numberOfLines = 0;
    _contentLabel.preferredMaxLayoutWidth = kScreen_Width - 110;
    [self.contentView addSubview:_contentLabel];
    
    _tweetPhotoView = [PYPhotosView photosView];
    _tweetPhotoView.userInteractionEnabled = YES;
    [self.contentView addSubview:_tweetPhotoView];
    
    _timeLabel = [UILabel labelWithSystemFontSize:12 textColor:[UIColor titleColor]];
    [self.contentView addSubview:_timeLabel];
    
    _commentButton = [UIButton buttonWithTitle:@"0" TitleColor:[UIColor titleColor] FontSize:14 ImageNameStr:@"ic_comment_30" BackgroundColor:nil];
    _commentButton.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 10);
    [self.contentView addSubview:_commentButton];
    
    [_iconView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.equalTo(self.contentView).offset(15);
        make.size.mas_equalTo(CGSizeMake(50, 50));
    }];
    [_nameLabel setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisVertical];
    [_nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView).offset(20);
        make.left.equalTo(self.iconView.mas_right).offset(20);
        make.right.equalTo(self.contentView).offset(20);
    }];
    [_contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.nameLabel.mas_bottom).offset(10);
        make.left.equalTo(self.nameLabel);
        make.right.equalTo(self.contentView).offset(-20);
    }];
    [_tweetPhotoView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentLabel.mas_bottom).offset(10);
        make.size.mas_equalTo(CGSizeZero);
        make.left.equalTo(self.contentLabel);
    }];
    [_commentButton setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
    [_commentButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.contentView).offset(-15);
        make.top.equalTo(self.tweetPhotoView.mas_bottom).offset(10);
    }];
    
    [_timeLabel setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisVertical];
    [_timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.commentButton);
        make.left.equalTo(self.contentLabel);
        make.right.equalTo(self.commentButton.mas_left);
        make.bottom.equalTo(self.contentView).offset(-10);
    }];
    
}

- (void)setupImageViewWithViewModel:(OSCTweetViewModel *)viewModel {
    // 相册view大小
    CGSize imageSize;
    NSInteger imageCount = viewModel.imgSmallURLArr.count;
    if (imageCount > 0) {
        [_tweetPhotoView setThumbnailUrls:viewModel.imgSmallURLArr];
        [_tweetPhotoView setOriginalUrls:viewModel.imgBigURLArr];
        if (imageCount == 1) {// 单图
            imageSize = CGSizeMake(_tweetPhotoView.photoWidth, _tweetPhotoView.photoHeight);
        } else {// 多图
            imageSize = [_tweetPhotoView sizeWithPhotoCount:viewModel.imgSmallURLArr.count photosState:1];
        }
    } else {// 没图
        imageSize = CGSizeZero;
    }
    // 更新约束
    [_tweetPhotoView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(imageSize);
    }];
    
}

- (void)bindViewModel:(OSCTweetViewModel *)viewModel {
    self.viewModel = viewModel;
}

- (void)setViewModel:(OSCTweetViewModel *)viewModel {
    [_iconView abb_loadImageWithUrlStr:viewModel.iconStr PlaceHolderImageStr:userDefaultImageStr Radius:25];
    _nameLabel.text = viewModel.author;
    _contentLabel.attributedText = viewModel.content;
    _timeLabel.text = viewModel.pubDate;
    [_commentButton setTitle:viewModel.commentCount forState:UIControlStateNormal];
    [self setupImageViewWithViewModel:viewModel];
}

@end
